package my.springboot.async;


import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Aspect
@Component
public class LogAdvice {

    @Pointcut("@annotation(my.springboot.async.Log)")
    private void log() {
    }

    @Around(value="log()")
    public Object aroundMethod(ProceedingJoinPoint point){
        Object result = null;
    	Signature signature = point.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        Log logInfo = targetMethod.getAnnotation(Log.class);
        String methodName = point.getSignature().getName();
        try {
        	log.debug("【{}】{}",methodName,logInfo.value());
            result = point.proceed();
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    
    @Pointcut("@annotation(org.springframework.scheduling.annotation.Async)")
    private void async() {
    }
    
    @Before(value="async()")
    public void before(){
    	log.debug("【执行异步任务】【开始】");
    }
    
    @After(value="async()")
    public void after(){
    	log.debug("【执行异步任务】【结束】");
    }
    
}
